<?php 
class DomDateTimeTestCase extends DomArTestUnitTestCase {
	
	public function testDate() {
		$t = strtotime('now');
		$a = new DomDateTime($t);
		
		$this->assertEqual($a->date('Y-m-d H:i:s'), date('Y-m-d H:i:s', $t ) );
	}
	
	public function testGetMonthFirstDay() {
		
		$t = strtotime('2009-03-10 01:01:01');
		$a = new DomDateTime( $t );
		$this->assertEqual( $a->getMonthFirstDay()->date('Y-m-d H:i:s'), '2009-03-01 00:00:00' );
		
	}
	
	public function testGetMonthLastDay() {
		
		$t = strtotime('2009-03-10 01:01:01');
		$a = new DomDateTime( $t );
		$this->assertEqual( $a->getMonthLastDay()->date('Y-m-d H:i:s'), '2009-03-31 23:59:59' );
		
	}
	
	public function testGetMonthPeriod() {
		
		$t = strtotime('2009-03-10 01:01:01');
		$s = strtotime('2009-03-01 00:00:00');
		$e = strtotime('2009-03-31 23:59:59');
		$a = new DomDateTime( $t );
		$p = $a->getMonthPeriod();
		
		$this->assertEqual( $p->getStart()->getStamp(), $s );
		$this->assertEqual( $p->getEnd()->getStamp(), $e );
		
	}

	public function test_Is() {
		
		$time = new DomDateTime('now');	
		$this->assertTrue( $time->is('now') );
		$this->assertTrue( $time->is('today') );
		$this->assertFalse( $time->is('tomorrow') );
		$this->assertFalse( $time->is('yesterday') );
		
		$time = new DomDateTime('+1 day');
		$this->assertFalse( $time->is('now') );
		$this->assertFalse( $time->is('today') );
		$this->assertTrue( $time->is('tomorrow') );
		$this->assertFalse( $time->is('yesterday') );	
		
		$time = new DomDateTime('-1 day');
		$this->assertFalse( $time->is('now') );
		$this->assertFalse( $time->is('today') );
		$this->assertFalse( $time->is('tomorrow') );
		$this->assertTrue( $time->is('yesterday') );
		
	}
	
	public function test_In() {
		
		$time = new DomDateTime('now');
		$this->assertTrue( $time->in('+1 day') );
		$this->assertTrue( $time->in('-1 day') );
		$this->assertFalse( $time->in('future') );
		$this->assertFalse( $time->in('past') );
		
		$time = new DomDateTime('+1 day');
		$this->assertTrue( $time->in('+1 day') );
		$this->assertFalse( $time->in('-1 day') );
		$this->assertTrue( $time->in('future') );
		$this->assertFalse( $time->in('past') );
		
		$time = new DomDateTime('-1 day');
		$this->assertFalse( $time->in('+1 day') );
		$this->assertTrue( $time->in('-1 day') );
		$this->assertFalse( $time->in('future') );
		$this->assertTrue( $time->in('past') );
	}

	public function testDomAr() {
		
		DomDateTimeTestClass::getTable()->update();
		
		$value = new DomDateTimeTestClass();
		$value->save();

		$a = strtotime('now');
		
		$this->assertDatabaseValue( $value->getDbTable(), 'datetime1', $value->id, date('Y-m-d H:i:s', $a ) );
		$this->assertDatabaseNull( $value->getDbTable(), 'datetime2', $value->id );
		$this->assertDatabaseValue( $value->getDbTable(), 'date1', $value->id,  date('Y-m-d', $a ) );
		$this->assertDatabaseNull( $value->getDbTable(), 'date2', $value->id );
		$this->assertDatabaseValue( $value->getDbTable(), 'date3', $value->id, '0000-00-00' );
		
		$this->assertIdentical( get_class($value->datetime1), 'DomAr_DateTime' );
		
		
		$value->datetime1 = '2009-10-10 20:10:10';

		$value->save();
		$this->assertDatabaseValue( $value->getDbTable(), 'datetime1', $value->id, '2009-10-10 20:10:10' );
		
		DomAr::flushStoredObjects();
		
		$value = DomDateTimeTestClass::load( $value->id );
		$this->assertIdentical( $value->datetime1->date(), '2009-10-10 20:10:10' );
		$this->assertIdentical( $value->datetime2->date('Y-m-d'), NULL );
		$this->assertIdentical( $value->date1->date(), date('Y-m-d', $a ) );
		$this->assertIdentical( $value->date2->date(), NULL );
		$this->assertIdentical( $value->date3->date(), '0000-00-00' );
		
	}


}
?>